package com.atguigu.cloud.service.impl;

import com.atguigu.cloud.entity.LovColumnConfig;
import com.atguigu.cloud.mapper.LovColumnConfigMapper;
import com.atguigu.cloud.service.LovColumnConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Lov列配置服务实现
 * 
 * @author atguigu
 * @since 2024-01-01
 */
@Service
public class LovColumnConfigServiceImpl extends ServiceImpl<LovColumnConfigMapper, LovColumnConfig>
        implements LovColumnConfigService {

    @Override
    public List<LovColumnConfig> getByLovConfigId(Long lovConfigId) {
        LambdaQueryWrapper<LovColumnConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LovColumnConfig::getLovConfigId, lovConfigId)
                .orderByAsc(LovColumnConfig::getColumnOrder);
        return list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveColumns(Long lovConfigId, List<LovColumnConfig> columns) {
        // 删除旧的列配置
        LambdaQueryWrapper<LovColumnConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LovColumnConfig::getLovConfigId, lovConfigId);
        remove(wrapper);

        // 保存新的列配置
        if (columns != null && !columns.isEmpty()) {
            for (LovColumnConfig column : columns) {
                column.setLovConfigId(lovConfigId);
            }
            return saveBatch(columns);
        }
        return true;
    }
}
